/*
* Copyright © Shanghai Awinic Technology Co., Ltd. 2020-2020. All rights reserved.
* Description: The header file of the Flash related driver library.
* Date： 20201126
* Awinic_Version： aw_soc_driver_v1.0.0
*/
#include "compile_option.h"
#ifdef AW_86802

#ifndef __AW8680X_FLASH_H
#define __AW8680X_FLASH_H

/* Include ------------------------------------------------------------------*/
#include "aw8680x_def.h"

enum flash_status
{
	FLASH_COMPLETE = 0,
	FLASH_FALL = 1,
	FLASH_BUSY = 2,
	FLASH_TIMEOUT = 3,
};
typedef enum flash_status FLASH_STATUS_TYPE_E;

#define NVR_ADDR_STAR				((AW_U32)0x02000000)
#define FLASH_PAGE_SIZE				(0x200)			// 512 bytes
#define ISP_RW_LEN					(0x07 << 4)
#define ADDR_DATA_LEN				((AW_U8)0x04)	// Register address occupies 4 bytes

#define FLASH_REG_DATA_LEN			(4)				// The register is of type int and the data is of type

/* The following are the definitions of related macros in register usage */
#define FLASH_EN				((AW_U32)0x00000017)
#define FLASH_CLOSE				((AW_U32)0xFFFFFFF8)

#define ISPCMD_DIS				((AW_U32)0x00000000)
#define ISPCMD_READ				((AW_U32)0x00000000)	// read flash
#define ISPCMD_WRITE			((AW_U32)0x00000002)	// write flash
#define ISPCMD_ERASE			((AW_U32)0x00000004)	// erase flash
#define ISPCMD_CHIP_EN			((AW_U32)0x00000100)	// chip=1 Select the whole chip
#define ISPCMD_CHIP_DIS			(~ISPCMD_CHIP_EN)		// chip=0 Select other address
#define ISPCMD_NVR_EN			((AW_U32)0x00000200)	// nvr=1 select the nvr sectors
#define ISPCMD_NVR_DIS			(~ISPCMD_NVR_EN)		// nvr=0 select the main arry sectors

#define FLASH_ISPGO				((AW_U32)0x00000001)
#define FLASH_SECTOR_NUM		(120)
#define FLASH_SECTOR_SIZE		(512)

#define FLASH_LENGTH_NUM(num)	((AW_U32)((num - 1) << 4))

void aw_close_device_irq(void);
void aw_open_device_irq(void);

#ifndef AWINIC_IAR
void flash_init(void);
FLASH_STATUS_TYPE_E flash_get_status(void);
FLASH_STATUS_TYPE_E flash_erase(AW_U32 addr_dat);
FLASH_STATUS_TYPE_E flash_erase_chip(void);
FLASH_STATUS_TYPE_E flash_erase_nvr(AW_U32 addr_dat);
FLASH_STATUS_TYPE_E flash_write_single_nvr(AW_U32 addr_dat, AW_U32 data_dat);
FLASH_STATUS_TYPE_E flash_write_many_nvr(AW_U32 addr_dat, AW_U8 *p_data_dat, AW_U8 reg_data_len);
FLASH_STATUS_TYPE_E flash_read_single_nvr(AW_U32 addr_dat, AW_U32 *p_data_dat);
FLASH_STATUS_TYPE_E flash_read_many_nvr(AW_U32 addr_dat, AW_U8 *p_data_dat, AW_U8 reg_data_len);
FLASH_STATUS_TYPE_E flash_write_single(AW_U32 addr_dat, AW_U32 data_dat);
FLASH_STATUS_TYPE_E flash_write_many(AW_U32 addr_dat, AW_U8 *p_data_dat, AW_U8 reg_data_len);
FLASH_STATUS_TYPE_E flash_read_single(AW_U32 addr_dat, AW_U32 *p_data_dat);
FLASH_STATUS_TYPE_E flash_read_many(AW_U32 addr_dat, AW_U8 *p_data_dat, AW_U8 reg_data_len);
#else
__ramfunc void flash_init(void);
__ramfunc FLASH_STATUS_TYPE_E flash_get_status(void);
__ramfunc FLASH_STATUS_TYPE_E flash_erase(AW_U32 addr_dat);
__ramfunc FLASH_STATUS_TYPE_E flash_erase_chip(void);
__ramfunc FLASH_STATUS_TYPE_E flash_erase_nvr(AW_U32 addr_dat);
__ramfunc FLASH_STATUS_TYPE_E flash_write_single_nvr(AW_U32 addr_dat, AW_U32 data_dat);
__ramfunc FLASH_STATUS_TYPE_E flash_write_many_nvr(AW_U32 addr_dat, AW_U8 *p_data_dat, AW_U8 reg_data_len);
__ramfunc FLASH_STATUS_TYPE_E flash_read_single_nvr(AW_U32 addr_dat, AW_U32 *p_data_dat);
__ramfunc FLASH_STATUS_TYPE_E flash_read_many_nvr(AW_U32 addr_dat, AW_U8 *p_data_dat, AW_U8 reg_data_len);
__ramfunc FLASH_STATUS_TYPE_E flash_write_single(AW_U32 addr_dat, AW_U32 data_dat);
__ramfunc FLASH_STATUS_TYPE_E flash_write_many(AW_U32 addr_dat, AW_U8 *p_data_dat, AW_U8 reg_data_len);
__ramfunc FLASH_STATUS_TYPE_E flash_read_single(AW_U32 addr_dat, AW_U32 *p_data_dat);
__ramfunc FLASH_STATUS_TYPE_E flash_read_many(AW_U32 addr_dat, AW_U8 *p_data_dat, AW_U8 reg_data_len);
#endif

#endif
#endif
